#include "connection.hpp"
#include <iostream>
#include <string>
#include <unistd.h>
using namespace std;

int main(int argc, char* args[]) {
    if (argc != 3) {
        cout << "Usage:\n\r" << args[0] <<  " server_ip server_port\n" << endl;
        return 0; 
    }
    string serverip = args[1];
    uint16_t serverport = stoi(args[2]);
    // 1、实例化异步工作线程对象
    rbmq::AsyncWorker::ptr _thread = make_shared<rbmq::AsyncWorker>();
    // 2、实例化连接对象
    rbmq::connection::ptr conn = make_shared<rbmq::connection>(serverip, serverport, _thread);
    // 连接服务器
    conn->connect();

    // 3、通过连接创建信道
    rbmq::channel::ptr chn = conn->openChannel();
    // 4、通过信道提供的服务完成所需业务
        // 1、声明一个交换机exchange1，交换机类型为广播模式
    google::protobuf::Map<std::string, std::string> temp_map;
    chn->declareExchange("exchange1", mqNs::ExchangeType::TOPIC, true, false, temp_map);
        // 2、声明两个队列queue1和queue2，
    chn->declareQueue("queue1", true, false, false, temp_map);
    chn->declareQueue("queue2", true, false, false, temp_map);
        // 3、绑定queue1和exchange1，binding_key为“queue1”
    chn->queueBinding("queue1", "exchange1", "queue1");
        // 3、绑定queue2和exchange1，binding_key为“news.music.#
    chn->queueBinding("queue2", "exchange1", "news.music.#");
    // 5、循环向交换机发布消息
    for (int i = 0; i < 10; i++) {
        string message = "hello world-" + to_string(i + 1);
        cout << "发布消息: " << message << endl;
        mqNs::BasicProperties bp;
        bp.set_id(UuidHelper::uuid());
        bp.set_delivery_mode(mqNs::DeliveryMode::DURABLE);
        bp.set_routing_key("news.music.pop");
        chn->basicPublish("exchange1", &bp, message);
        sleep(1);
    }

    string message = "hello sport";
    cout << "发布消息: " << message << endl;
    mqNs::BasicProperties bp;
    bp.set_id(UuidHelper::uuid());
    bp.set_delivery_mode(mqNs::DeliveryMode::DURABLE);
    bp.set_routing_key("news.music.sport");
    chn->basicPublish("exchange1", &bp, message);

    bp.set_routing_key("news.sport");
    chn->basicPublish("exchange1", &bp, "你看不到我, 嘿嘿嘿!!!");
    // 6、关闭信道
    conn->closeChannel(chn);
    return 0;
}